<?php

namespace App\Services;

use App\Models\Address;
use App\Models\Area;
use App\Models\ElectricAccountingRecords;
use App\Models\ElectricPaymentOrder;
use App\Models\Users;
use EasyWeChat\Payment\Application;

class WePaySplitService extends BaseService
{

    /**
     * 2021/3/20--15:03
     * @effect:获取微信商户分账收款方数组
     * @param $owner
     * @param $order
     * @return array|string
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:getWePayReceivers
     */
    public function getWePayReceivers($owner, $order)
    {
        $options = [
            'type' => $owner['wechat_account_type'],
            'account' => $owner['wechat_account'],
            'relation_type' => 'DISTRIBUTOR'

        ];
        $amount = $order['split_money'] * 100;//转化分账金额为分
        $receivers = [
            'type' => $owner['wechat_account_type'],
            'account' => $owner['wechat_account'],
            'amount' => $amount,
            'description' => '分账给代理商',
        ];
        $result = app("wechat_split")->profit_sharing->addReceiver($options);
        if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
            return $receivers;
        }
        return $result['return_code'] . $result['err_code_des'];
    }

    /**
     * 2021/3/20--15:08
     * @effect:分账后操作
     * @param $arr
     * @param array $order
     * @param $agent
     * @return mixed
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:afterSplit
     */
    public static function afterSplit($arr, array $order, $agent)
    {
        $owner = Users::selectRaw('room_number')->find($order['user_id']);//查询业主房间信息
        ElectricPaymentOrder::where(['orderNo' => $arr['orderNo']])->update(['is_split' => 2]);
        $data = [
            'agent_id' => $agent['id'],//代理商id
            'money' => $order['split_money'],//分账金额
            'number' => $owner['room_number'],//房间号
            'type' => $order['pay_type'],//缴费类型
            'orderNo' => $arr['orderNo'],
            'handlingFee' => $order['handlingFee'],//手续费
            'afterSplit' => $order['after_split'],//分账后余额
            'from_user' => $order['user_id'],
            'status' => 2
        ];
        return ElectricAccountingRecords::create($data);
    }

    /**
     * 2021/3/20--15:00
     * @effect:微信分账公共方法
     * @param $p
     * @return array
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:WePaySplitDo
     */
    public function WePaySplitDo($p): array
    {
        extract($p, null);
        $one_order = $p['one_order'];//订单
        $agent = $p['agent'];
        $pay_app = new Application();
        $pay_app->setSubMerchant($agent['sub_mch_id']);
        $receivers = $this->getWePayReceivers($agent, $one_order);//添加收款方
        if (isset($receivers['code'])) {
            return ['return_code' => 'FAIL', 'result_code' => 'FAIL', 'err_code_des' => $receivers['msg']];
        }

        $result = app('wechat_split')->profit_sharing->share($one_order['pay_no'], $one_order['orderNo'], $receivers);//发起分账
        if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
            return $result;
        }

        return $result;
    }

}
